﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;

namespace Common
{
    public class DbBase
    {

        public bool Insert<T>(IDbConnection conn, string sql, T model)
        {
            var result = conn.Execute(sql,model);
            return result > 0;
        }
        public bool InsertBatch<T>(IDbConnection conn, string sql, List<T> model)
        {
            return conn.Execute(sql, model) > 0;
        }
        public bool Update<T>(IDbConnection conn, string sql, T model)
        {
            var result = conn.Execute(sql, model);
            return result > 0;
        }
        public bool UpdateBatch<T>(IDbConnection conn, string sql, List<T> model)
        {
            var result = 0;
            IDbTransaction transaction = conn.BeginTransaction();
            try
            {
                foreach (var item in model)
                {
                    result = conn.Execute(sql, item, transaction, null, null);
                    if (result < 1)
                    {
                        break;
                    }
                }
                if (result > 0)
                {
                    transaction.Commit();
                }
                else
                {
                    transaction.Rollback();
                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                return false;
            }
            return result > 0;
        }
        public bool Delete<T>(IDbConnection conn, string sql, T model)
        {
            var result = conn.Execute(sql, model);
            return result > 0;
        }
        public bool Delete(IDbConnection conn, string sql, object model = null)
        {
            var result = conn.Execute(sql, model);
            return result > 0;
        }
        public bool DeleteList(IDbConnection conn, string sql, List<object> model)
        {
            var result = 0;
            IDbTransaction transaction = conn.BeginTransaction();
            try
            {
                foreach(var item in model)
                {
                    result = conn.Execute(sql, item, transaction, null, null);
                    if(result < 1)
                    {
                        break;
                    }
                }
                if (result > 0)
                {
                    transaction.Commit();
                }
                else
                {
                    transaction.Rollback();
                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                return false;
            }
            return result > 0;
        }
        public bool DeleteList<T>(IDbConnection conn, string sql, List<T> model)
        {
            var result = 0;
            IDbTransaction transaction = conn.BeginTransaction();
            try
            {
                foreach (var item in model)
                {
                    result = conn.Execute(sql, item, transaction, null, null);
                    if (result < 1)
                    {
                        break;
                    }
                }
                if (result > 0)
                {
                    transaction.Commit();
                }
                else
                {
                    transaction.Rollback();
                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                return false;
            }
            return result > 0;
        }

        public bool Execute(IDbConnection conn,string sql,object model = null)
        {
            var result = conn.Execute(sql, model);
            return result > 0;
        }

        public bool ExecuteList(IDbConnection conn, List<string> sqlList, List<object> model = null)
        {
            var result = 0;
            IDbTransaction transaction = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < sqlList.Count; i++)
                {
                    result = conn.Execute(sqlList[i], model[i], transaction, null, null);
                    if (result < 1)
                    {
                        break;
                    }
                }
                if (result > 0)
                {
                    transaction.Commit();
                }
                else
                {
                    transaction.Rollback();
                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                return false;
            }
            return result > 0;
        }
        public int GetCount(IDbConnection conn, string sql, object model = null)
        {
            var count = conn.Query<int>(sql, model).Single();
            return count;
        }
        public bool Exists<T>(IDbConnection conn, string sql, object model = null)
        {
            var entity = conn.Query<T>(sql, model).FirstOrDefault();
            if (entity == null)
            {
                return false;
            }
            return true;
        }
        public T GetEntity<T>(IDbConnection conn, string sql, object model = null)
        {
            var entity = conn.Query<T>(sql, model).FirstOrDefault();
            return entity;
        }

        public List<T> GetList<T>(IDbConnection conn, string sql, object model = null)
        {
            var list = conn.Query<T>(sql, model).ToList();
            return list;
        }
        public PageHelper<T> GetPageList<T>(IDbConnection conn, int pageIndex, int pageSize, string tableName, string where = "where 1=1", string orderBy = "order by id", string returnFields = "*")
        {
            var parameters = new DynamicParameters();
            parameters.Add("@PageCount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            parameters.Add("@RecordCount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            parameters.Add("@TableName", tableName, direction: ParameterDirection.Input);
            parameters.Add("@ReturnFields", returnFields, direction: ParameterDirection.Input);
            parameters.Add("@PageSize", pageSize, direction: ParameterDirection.Input);
            parameters.Add("@PageIndex", pageIndex, direction: ParameterDirection.Input);
            parameters.Add("@Where", string.Format(" {0} ", where), direction: ParameterDirection.Input);
            parameters.Add("@OrderBy", string.Format(" {0} ", orderBy), direction: ParameterDirection.Input);
            var result = conn.Query<T>("WEB_PageView", parameters, commandType: CommandType.StoredProcedure).ToList();
            int pageCount = parameters.Get<int>("@PageCount");
            int recordCount = parameters.Get<int>("@RecordCount");
            PageHelper<T> model = new PageHelper<T>();
            model.PageCount = pageCount;
            model.PageIndex = pageIndex;
            model.PageSize = pageSize;
            model.RecordCount = recordCount;
            model.PageData = result;
            return model;
        }
    }
}
